package com.travel.api.controller.information

import com.lgy.sell.controller.BaseController
import com.travel.api.beans.response.information.HomeResponse
import com.travel.api.beans.information.home.Crunchies
import com.travel.api.beans.information.home.Icon
import com.travel.api.beans.information.home.Recommend
import com.travel.api.beans.information.home.Swiper
import com.travel.api.beans.information.home.Weekend
import com.travel.api.service.information.MongoCommonService
import com.travel.api.utils.CommonUtil
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

/**
 * @ClassName HomeController
 * @Author liguangyao
 * @Date 27/8/18 下午9:46
 * @Version 1.0
 */
@Api(value = "/information/home", description = "Travel首页")
@RestController
@RequestMapping("information/home")
@CrossOrigin
class HomeController : BaseController() {

    @Autowired
    private val mongoCommonService: MongoCommonService? = null

    @PostMapping("/saveCrunchies")
    @ApiOperation(value = "Travel首页热榜组件数据保存", httpMethod = "POST", notes = "Travel首页热榜组件数据保存")
    fun saveCrunchies(@RequestBody s: List<Crunchies>): Map<String, Any> {
        mongoCommonService!!.saveCollection(s)
        return success()
    }

    @GetMapping("/listCrunchies")
    @ApiOperation(value = "获取Travel首页热榜组件数据集", httpMethod = "GET", notes = "获取Travel首页热榜组件数据集")
    fun listCrunchies(): Map<String, Any> {
        return success(null, mongoCommonService!!.selectCollectionByName(Crunchies::class.java, "crunchies"))
    }

    @PostMapping("/saveIcon")
    @ApiOperation(value = "Travel首页图标组件数据保存", httpMethod = "POST", notes = "Travel首页图标组件数据保存")
    fun saveIcon(@RequestBody s: List<Icon>): Map<String, Any> {
        mongoCommonService!!.saveCollection(s)
        return success()
    }

    @GetMapping("/listIcon")
    @ApiOperation(value = "获取Travel首页图标组件数据集", httpMethod = "GET", notes = "获取Travel首页图标组件数据集")
    fun listIcon(): Map<String, Any> {
        return success(null, mongoCommonService!!.selectCollectionByName(Icon::class.java, "icon"))
    }

    @PostMapping("/saveRecommend")
    @ApiOperation(value = "Travel首页产品推荐组件数据保存", httpMethod = "POST", notes = "首页产品推荐组件保存")
    fun saveRecommend(@RequestBody s: List<Recommend>): Map<String, Any> {
        mongoCommonService!!.saveCollection(s)
        return CommonUtil.getSuccessResult()
    }

    @GetMapping("/listRecommend")
    @ApiOperation(value = "获取Travel首页产品推荐组件数据集", httpMethod = "GET", notes = "获取首页产品推荐组件数据集")
    fun listRecommend(): Map<String, Any> {
        return success(null, mongoCommonService!!.selectCollectionByName(Recommend::class.java, "recommend"))
    }

    @PostMapping("/saveSwiper")
    @ApiOperation(value = "Travel首页轮播图组件数据保存", httpMethod = "POST", notes = "Travel首页轮播图组件数据保存")
    fun saveSwiper(@RequestBody s: List<Swiper>): Map<String, Any> {
        mongoCommonService!!.saveCollection(s)
        return success()
    }

    @GetMapping("/listSwiper")
    @ApiOperation(value = "获取Travel首页轮播图组件数据集", httpMethod = "GET", notes = "获取Travel首页轮播图组件数据集")
    fun listSwiper(): Map<String, Any> {
        return success(null, mongoCommonService!!.selectCollectionByName(Swiper::class.java, "swiper"))
    }

    @PostMapping("/saveWeekend")
    @ApiOperation(value = "Travel首页周末组件数据保存", httpMethod = "POST", notes = "Travel首页周末组件数据保存")
    fun saveWeekend(@RequestBody s: List<Weekend>): Map<String, Any> {
        mongoCommonService!!.saveCollection(s)
        return success()
    }

    @GetMapping("/listWeekend")
    @ApiOperation(value = "Travel首页周末组件数据集", httpMethod = "GET", notes = "Travel首页周末组件数据集")
    fun listWeekend(): Map<String, Any> {
        return success(null, mongoCommonService!!.selectCollectionByName(Weekend::class.java, "weekend"))
    }

    @ApiOperation(value = "Travel首页数据信息数据集", httpMethod = "GET", notes = "Travel首页数据信息数据集")
    @GetMapping("/list")
    fun indexList(): Map<String, Any> {

        val crunchiesList = mongoCommonService!!
                .selectCollectionByName(Crunchies::class.java, "crunchies")
        val iconList = mongoCommonService
                .selectCollectionByName(Icon::class.java, "icon")
        val recommendList = mongoCommonService
                .selectCollectionByName(Recommend::class.java, "recommend")
        val swiperList = mongoCommonService
                .selectCollectionByName(Swiper::class.java, "swiper")
        val weekendList = mongoCommonService
                .selectCollectionByName(Weekend::class.java, "weekend")

        val homeResponse = HomeResponse(
                crunchiesList as List<Crunchies>?,
                iconList as List<Icon>?,
                recommendList as List<Recommend>?,
                swiperList as List<Swiper>?,
                weekendList as List<Weekend>?
        )
        return success(null, homeResponse)
    }
}
